package com.cpi.cornpp.balanceversion.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.balanceversion.exception.BalanceVersionException;
import com.cpi.cornpp.balanceversion.form.BalanceVersionForm;
import com.cpi.cornpp.balanceversion.services.BalanceVersionService;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cs.common.utils.NumberUtil;


public class BalanceVersionAction extends MappingDispatchAction {
	/** DEFAULT Navigation */
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Version Id Parameter */
	private static final String VERSION_ID_PARAMETER = "versionId";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Error message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward showBalances(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		BalanceVersionForm balanceVersionForm = null;
		HttpSession httpSession = null;
		int versionId = 0;
		BalanceVersionService balanceVersionService = null;
		List balanceVersionsList = null;
		try{
			balanceVersionForm = (BalanceVersionForm) form;
			httpSession = request.getSession(true);
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_PARAMETER));
			balanceVersionsList = new ArrayList(0);
			balanceVersionService = new BalanceVersionService();		
			balanceVersionsList = balanceVersionService.getBalanceVersions(versionId);
			balanceVersionForm.setBalanceVersionList(balanceVersionsList);					
		}catch (BalanceVersionException balanceVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, balanceVersionException);
			throw balanceVersionException;
		}
		catch (Exception e){
			BalanceVersionException balanceVersionException=null;
			e.printStackTrace(System.out);
			balanceVersionException= new BalanceVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, balanceVersionException);
			throw balanceVersionException;
		}	
		return mapping.findForward(DEFAULT);
	}
	/**
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveBalancesVersions(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		BalanceVersionForm balanceVersionForm = null;
		HttpSession httpSession = null;		
		BalanceVersionService balanceVersionService = null;
		MessageResources messageResources = null;
		String messageError = null;		
		try{
			balanceVersionForm = (BalanceVersionForm) form;
			messageResources = getResources(request);			
			httpSession = request.getSession(true);						
			balanceVersionService = new BalanceVersionService();					
			balanceVersionService.updateBalansVersions(balanceVersionForm.getBalanceVersionList());
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);	
		}catch (BalanceVersionException balanceVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, balanceVersionException);
			throw balanceVersionException;
		}
		catch (Exception e){
			BalanceVersionException balanceVersionException=null;
			e.printStackTrace(System.out);
			balanceVersionException= new BalanceVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, balanceVersionException);
			throw balanceVersionException;
		}	
		return mapping.findForward(DEFAULT);
	}
}